import React, { Component, createRef } from "react";

class Child extends Component {
  state = {
    count: 1,
  };
  render() {
    console.log(this);
    return <div>child组件</div>;
  }
}

class App extends Component {
  state = {};
  cd = createRef();
  fn = () => {
    // this.cd.current就是子组件的实例
    // 和子组件里面打印的this是一样的
    // 就可以在父组件拿到子组件的属性和方法
    console.log(this.cd.current.state.count);
  };
  render() {
    return (
      <>
        <h2>ref用来获取组件实例</h2>
        <Child ref={this.cd} />
        <button onClick={this.fn}>btn</button>
      </>
    );
  }
}

export default App;
